import Vue from 'vue'
import Vuex from 'vuex'
import axios from '../utils/axios1'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[]  //表格数据
  },
  mutations: {
    setList(state,payload){
      state.list=payload
    },
    // 添加商品
    addNums(state,payload){
      state.list.unshift(payload)
    },
    // 编辑店铺
    edit(state,payload){
      let lists=[...state.list];
      let newList=[];
      lists.map(item=>{
        if(item.id==payload.id){
           newList.push(payload)
        }else{
          newList.push(item)
        }
      })
      state.list=newList
    },
    // 删除
    del(state,payload){
       let arr=[];
       state.list.forEach((item)=>{
         if(item.id!=payload.id){
           arr.push(item)
         }
       })
       state.list=arr
    }
  },
  actions: {
     getList(context){
       axios.get('/api/tabble').then(res=>{
          context.commit('setList',res.data.data)
       })
     }
  },
  modules: {
  }
})
